package com.ld.hpfs.view.excel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.servlet.view.document.AbstractExcelView;

import com.ld.hpfs.web.interseptor.WebConstants;

public class SearchResultExcelView extends AbstractExcelView{
	@Override
    protected void buildExcelDocument(Map model, HSSFWorkbook workbook, HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        // // 取得模型数据
        // SqlRowSet table = (SqlRowSet) model.get("table");
        // String title = model.get("title").toString();

        // 取得模型数据
    	ArrayList<HashMap<String,Object>> table = (ArrayList<HashMap<String,Object>>) model.get("table");
 //       String title = "代码分析表";

        // 创建工作表和标题
        HSSFSheet sheet = workbook.createSheet(); // 创建工作区
//        HSSFRow row_title = sheet.createRow(0); // 创建一行引用对象
//        HSSFFont title_font = workbook.createFont(); // 创建标题的字体
//
//        title_font.setFontHeightInPoints((short) 8);
//        title_font.setFontHeight((short) HSSFFont.BOLDWEIGHT_NORMAL);
//        title_font.setColor((short) (HSSFFont.COLOR_RED));
//
//        HSSFCellStyle title_style = workbook.createCellStyle(); // 创建样式
//        title_style.setFont(title_font);
//
//        HSSFCell cell_title = row_title.createCell(1); // 创建单元格引用对象
//        cell_title.setCellStyle(title_style);
//        cell_title.setCellValue(title);

//        // 创建数据表头
//        String titles[] = { "科目编码", "ERP凭证号","凭证编号","借方金额","贷方金额","过账日期","币种","描述"};

     // 创建数据表头
        String titles[] = { "凭证日期", "过账日期", "凭证状态", "凭证类型","ERP凭证号","原始摘要"
        		,"翻译摘要","科目代码","科目名称","ERP科目","ERP科目名称","币种","借方金额","贷方金额","制单人","T-code1","T-code2","T-code3"
        		,"T-code4","T-code5","T-code6","T-code7","T-code8","T-code9","T-code10","相关字段1","相关字段2","相关字段3","相关字段4"
        		,"相关字段5","相关字段6","相关字段7","相关字段8","相关字段9","相关字段10"};
        
        HSSFRow row = sheet.createRow(0);
        HSSFCellStyle items_style = workbook.createCellStyle();
        items_style.setAlignment((short) HSSFCellStyle.ALIGN_CENTER);

        HSSFFont celltbnamefont = workbook.createFont();
        celltbnamefont.setFontHeightInPoints((short) 10);

        celltbnamefont.setColor((short) (HSSFFont.COLOR_RED));
        items_style.setFont(celltbnamefont);
        items_style.setWrapText(true);

        for (int i = 0; i < titles.length; i++) {
            HSSFCell cell = row.createCell(i);
            sheet.setColumnWidth(i, 5335);
            cell.setCellValue(titles[i]);
            cell.setCellStyle(items_style);
        }

        HSSFCellStyle datestyle = workbook.createCellStyle();
        HSSFDataFormat df = workbook.createDataFormat();
        datestyle.setDataFormat(df.getFormat("yyyy-mm-dd"));
        String lang = request.getSession().getAttribute(WebConstants.CURRENT_LANG).toString();          
        int rowNum = 1;
        for (HashMap<String,Object> entry : table) {
            // create the row data
            HSSFRow datarow = sheet.createRow(rowNum++);
            if(entry.get("ENTRY_DATE")!=null){
            	datarow.createCell(0).setCellValue(entry.get("ENTRY_DATE").toString());
            }
            if(entry.get("POSTING_DATE")!=null){
            	datarow.createCell(1).setCellValue(entry.get("POSTING_DATE").toString());
            }
            if(entry.get("POST_STATUS")!=null){
            	if(entry.get("POST_STATUS").toString().equals("1")){
            		if(lang.equals("en")){
            			datarow.createCell(2).setCellValue("post");
            		} else {
            			datarow.createCell(2).setCellValue("已过账");
            		}
            	} else {
            		if(lang.equals("en")){
            			datarow.createCell(2).setCellValue("unpost");
            		} else {
            			datarow.createCell(2).setCellValue("未过账");
            		}
            	}
            }
            if(entry.get("IMPORT_TYPE")!=null){
            	if(entry.get("IMPORT_TYPE").toString().equals("1")){
            		if(lang.equals("en")){
            			datarow.createCell(3).setCellValue("modify");
            		} else {
            			datarow.createCell(3).setCellValue("调整凭证");
            		}
            	} else {
            		if(lang.equals("en")){
            			datarow.createCell(3).setCellValue("import");
            		} else {
            			datarow.createCell(3).setCellValue("导入凭证");
            		}
            	}
            }
            if(entry.get("ENTRY_NO")!=null){
            	datarow.createCell(4).setCellValue(entry.get("ENTRY_NO").toString());
            }
            if(entry.get("SENTRY_DESC1")!=null){
            	datarow.createCell(5).setCellValue(entry.get("SENTRY_DESC1").toString());
            }
            if(entry.get("CHINA_DESC")!=null){
            	datarow.createCell(6).setCellValue(entry.get("CHINA_DESC").toString());
            }
            if(entry.get("LOCAL_ACCOUNT")!=null){
            	datarow.createCell(7).setCellValue(entry.get("LOCAL_ACCOUNT").toString());
            }
            if(entry.get("LOCAL_ACCOUNT_DESC")!=null){
            	datarow.createCell(8).setCellValue(entry.get("LOCAL_ACCOUNT_DESC").toString());
            }
            if(entry.get("ERP_ALL_ACCOUNT")!=null){
            	datarow.createCell(9).setCellValue(entry.get("ERP_ALL_ACCOUNT").toString());
            }
            if(entry.get("ERP_ALL_ACCOUNT_DESC")!=null){
            	datarow.createCell(10).setCellValue(entry.get("ERP_ALL_ACCOUNT_DESC").toString());
            }
            if(entry.get("LCURRENCY")!=null){
            	datarow.createCell(11).setCellValue(entry.get("LCURRENCY").toString());
            }
            if(entry.get("AMOUNT_LOCALDB")!=null){
            	datarow.createCell(12).setCellValue(entry.get("AMOUNT_LOCALDB").toString());
            }
            if(entry.get("AMOUNT_LOCALCR")!=null){
            	datarow.createCell(13).setCellValue(entry.get("AMOUNT_LOCALCR").toString());
            }
            if(entry.get("OPERATER")!=null){
            	datarow.createCell(14).setCellValue(entry.get("OPERATER").toString());
            }
            if(entry.get("REFRENCE1")!=null){
            	datarow.createCell(15).setCellValue(entry.get("REFRENCE1").toString());
            }
            if(entry.get("REFRENCE2")!=null){
            	datarow.createCell(16).setCellValue(entry.get("REFRENCE2").toString());
            }
            if(entry.get("REFRENCE3")!=null){
            	datarow.createCell(17).setCellValue(entry.get("REFRENCE3").toString());
            }
            if(entry.get("REFRENCE4")!=null){
            	datarow.createCell(18).setCellValue(entry.get("REFRENCE4").toString());
            }
            if(entry.get("REFRENCE5")!=null){
            	datarow.createCell(19).setCellValue(entry.get("REFRENCE5").toString());
            }
            if(entry.get("REFRENCE6")!=null){
            	datarow.createCell(20).setCellValue(entry.get("REFRENCE6").toString());
            }
            if(entry.get("REFRENCE7")!=null){
            	datarow.createCell(21).setCellValue(entry.get("REFRENCE7").toString());
            }
            if(entry.get("REFRENCE8")!=null){
            	datarow.createCell(22).setCellValue(entry.get("REFRENCE8").toString());
            }
            if(entry.get("REFRENCE9")!=null){
            	datarow.createCell(23).setCellValue(entry.get("REFRENCE9").toString());
            }
            if(entry.get("REFRENCE10")!=null){
            	datarow.createCell(24).setCellValue(entry.get("REFRENCE10").toString());
            }
            if(entry.get("REFRENCE11")!=null){
            	datarow.createCell(25).setCellValue(entry.get("REFRENCE11").toString());
            }
            if(entry.get("REFRENCE12")!=null){
            	datarow.createCell(26).setCellValue(entry.get("REFRENCE12").toString());
            }
            if(entry.get("REFRENCE13")!=null){
            	datarow.createCell(27).setCellValue(entry.get("REFRENCE13").toString());
            }
            if(entry.get("REFRENCE14")!=null){
            	datarow.createCell(28).setCellValue(entry.get("REFRENCE14").toString());
            }
            if(entry.get("REFRENCE15")!=null){
            	datarow.createCell(29).setCellValue(entry.get("REFRENCE15").toString());
            }
            if(entry.get("REFRENCE16")!=null){
            	datarow.createCell(30).setCellValue(entry.get("REFRENCE16").toString());
            }
            if(entry.get("REFRENCE17")!=null){
            	datarow.createCell(31).setCellValue(entry.get("REFRENCE17").toString());
            }
            if(entry.get("REFRENCE18")!=null){
            	datarow.createCell(32).setCellValue(entry.get("REFRENCE18").toString());
            }
            if(entry.get("REFRENCE19")!=null){
            	datarow.createCell(33).setCellValue(entry.get("REFRENCE19").toString());
            }
            if(entry.get("REFRENCE20")!=null){
            	datarow.createCell(34).setCellValue(entry.get("REFRENCE20").toString());
            }
        }
    }
}
